
<!-- 进度模板 -->
<template>
	<view :class="{showModal: showModal}">
		<u-sticky offset-top="0">
			<common-search
				class="search"
				style="width: 100%;"
				v-model="queryParam.templateName" 
				:screenShow="screenShow"
				@screenclick="screenclick"
				@searchMsg="searchMsg"
				placeholder="请输入模板名称"
			></common-search>
		</u-sticky>
		<view class="page" :style="!isData?'display:flex;width: 100vw;height:100vh;align-items:center;justify-content: center;':''">
			<template v-if="isData">
				<view class="list" v-for="(item,index) in dataList" :key="index" @click="onChecker(item, index)">
					<view class="">
						<view class="option">
							<view class="lf">模板编号</view>
							<view class="rl">{{ item.templateNo }}</view>
						</view>
						<view class="option">
							<view class="lf">模板名称</view>
							<view class="rl">{{ item.templateName }}</view>
						</view>
						<view class="option">
							<view class="lf">业务分类</view>
							<view class="rl">{{ item.bussinessType_dictText }}</view>
						</view>
					</view>
					<view class="icon" v-show="item.isChecker"><u-icon name="checkbox-mark" color="#2979ff" size="22"></u-icon></view>
				</view>
				<!-- 加载更多显示 -->
				<view class=""><u-loadmore :status="statusLoadmore" /></view>
			</template>
			<view v-else>
				<u-empty
					mode="data"
					icon="/static/empty/data.png"
				>
				</u-empty>
			</view>
			<view style="height: 100rpx;"></view>
			<view class="btn">
				<u-button text="确定" type="primary" @click="onConfirm"></u-button>
			</view>
		</view>
		
		<u-toast ref="uToast"></u-toast>
		<u-popup :show="showModal" @close="confirmModal" mode="bottom" :round="20" closeable>
				<h4 class="slot-title">进度模板</h4>
				<scroll-view class="slot-content" scroll-y="true" @touchmove.stop>
					<u-steps :current="-1" direction="column">
						<u-steps-item 
							v-for="(item, index) in nodeList"
							:key="index"
							:title="item.nodeName" 
							:desc="item.nodeProgress ? item.nodeProgress + '%': 0 + '%'"
						></u-steps-item>
					</u-steps>
			</scroll-view>
		</u-popup>
	</view>
</template>
<script>
	import { erpProgressTemplateList, erpProgressTemplateDtList } from '@/api/index.js';
	import CommonSearch from '../../components/CommonSearch.vue'
	export default {
		components: {
			CommonSearch
		},
		data() {
			return {
				objectFun: '', // 方法
				showNotify: true, // 消息提示
				showModal: false, // 弹窗
				id: '', // 选中的id
				nodeList: [], // 模板列博鳌
				page: {
					pageNo: 1,
					pageSize: 10,
				},
				dataList: [] ,// 列表
				isData: true, // 判断是否有数据  true 代表有 false 代表无
				statusLoadmore: 'loadmore', // 通过status设置组件的状态，加载前值为loadmore，加载中为loading，没有数据为nomore
				oldIndex: 0, // 存储列表之前被选中的数据
				screenShow: false, // 筛选弹窗
				
				// 传的参数
				queryParam: {
					templateName: ''
				},
				
				objectField: '', // 点击确认需要修改的字段
			}
		},
		onLoad(options) {
			this.id = options.id || ''
			this.objectField = options.objectField || ''
			this.isMulti = options.isMulti || ''
			this.objectFun = options.objectFun || ''
			this.geterpOmcTeamManagerList() // 获取列表
		},
		methods: {
			// 获取列表
			geterpOmcTeamManagerList () {
				this.statusLoadmore = 'loading' // 加载中
				var params = {
					column: 'createTime',
					order: 'desc',
					pageNo: this.page.pageNo,
					pageSize: this.page.pageSize,
					
				}
				if (this.queryParam.templateName) {
					params.templateName = '*' + this.queryParam.templateName + '*'
				}
				erpProgressTemplateList(params).then(res => {
					if (res.success) {
						 // 判断首次加载是否有数据
						if (this.page.pageNo == 1){
							if (res.result.records&&res.result.records.length) {
								this.isData = true
							} else {
								this.isData = false
							}
						}
						if (res.result.records&&res.result.records.length){
							if (res.result.records.length >= 10 ) {
								this.statusLoadmore = 'loadmore'
							} else {
								this.statusLoadmore = 'nomore'
							}
							// 处理数据
							this.oldIndex = 0
							var empIdArr = this.id.split(',')
							if (empIdArr.length == 1) {
								res.result.records.map((value, index) => {
									if (this.id == value.id) {
										value.isChecker = true
									} else {
										value.isChecker = false
									}
									return value
								})
							} else {
								res.result.records.map((value, index) => {
									// 是否选中数据
									value.isChecker = false
									empIdArr.map((item,ind) => {
										if (item == value.id) {
											value.isChecker = true
											// this.oldIndex = index // 当单选的时候
										}
									})
									
									return value
								})
							}
							
						}else {
							this.statusLoadmore = 'nomore'
						}
						if (this.page.pageNo == 1){
							this.dataList = res.result.records
						} else {
							this.dataList = this.dataList.concat(res.result.records)
						}
						if (this.dataList && this.dataList.length && this.isMulti != 'true') {
							const oldIndex =	this.dataList.findIndex(item => item.isChecker)
							if (oldIndex == -1) {
								this.oldIndex = 0
							} else {
								this.oldIndex = oldIndex
							}
						}
					} else {
						this.isData = false
						this.dataList = []
						this.statusLoadmore = 'nomore'
					}
				})
			},
			
			// 点击筛选
			screenclick () {
				this.screenShow = !this.screenShow
			},
			// 输入内容
			searchMsg () {
				this.page.pageNo = 1 // 页数归1
				this.dataList = [] // 清空列表
				this.geterpOmcTeamManagerList() // 获取列表
			},
			
			// 点击选中取消选中
			onChecker(item, index) {
				if (this.isMulti == 'true') {
					this.dataList[index].isChecker = !this.dataList[index].isChecker // 多选
					// 判断当前列表有没有选中的id值
					this.onIsChecker(this.dataList[index])
					
				} else {
					// 单选
					// 判断是否已经被选中了
					if (item.isChecker) {
						// 当已经被选中了
						this.dataList[index].isChecker = !this.dataList[index].isChecker
						this.id = ''
					} else {
						// 当没有被选中
						
						// 修改之前被选中的状态
						if (this.dataList[this.oldIndex].isChecker) {
							this.dataList[this.oldIndex].isChecker = false
						}
						// 修改现在的状态
						this.dataList[index].isChecker = true // 变成选中的状态
						this.oldIndex = index
						this.id = item.id
						this.showModal = true
						this.nodeList = []
						this.getTemplateDtList(item.id)
						
					}
				}
			},
			
			// 获取进度模板
			getTemplateDtList(mainId) {
				erpProgressTemplateDtList({mainId}).then(res => {
					if (res.success) {
						this.nodeList = res.result
					}
				})
			},
			// 用于判断修改当前的id值
			onIsChecker(item) {
				// 判断当前值是否被选中
				if (item.isChecker) {
					// 被选中,直接拼接id值
					if (this.id) {
						this.id = this.id + ',' + item.id
					} else {
						this.id = item.id
					}
					
				} else {
					// 当被取消选中,则在id值中去除此id值
					
					var empIdArr = this.id.split(',')
					if (empIdArr.length == 1) {
						// 当id值只有一个时则直接清除
						this.id = ''
					} else {
						// 当id值是多个的时候
						empIdArr.map((val,ind) => {
							if (val == item.id) {
								empIdArr.splice(ind, 1) // 删除当前的id值
							}
						})
						this.id = empIdArr.toString()
					}
				}
			},
			
			// 点击弹窗
			confirmModal () {
				this.showModal = false
			},
			
			// 点击确定
			onConfirm () {
				var item = this.id
				if (item) {
					let arr = this.objectField.split('.')
					let pages = getCurrentPages() //获取所有页面栈实例列表
					let nowPage = pages[pages.length - 1] //当前页页面实例
					let prevPage = pages[pages.length - 2] //上一页页面实例
					if (prevPage.$vm) {
						if (arr.length == 1) {
							prevPage.$vm[this.objectField] = item //修改上一页data里面的tagIndex 参数值
						} else if (arr.length == 2) {
							prevPage.$vm[arr[0]][arr[1]] = item //修改上一   页data里面的tagIndex 参数值
						}
						if (this.objectFun) {
							prevPage.$vm[this.objectFun]({id: item})
						}
					}
					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
					})
				} else {
					this.$refs.uToast.show({
						type: 'error',
						icon: 'none',
						message: '请选择数据'
					})
				}
			}
			
			
		},
		onReachBottom() {
			if (this.statusLoadmore == 'nomore') return false
			this.page.pageNo = ++ this.page.pageNo;
			this.geterpOmcTeamManagerList()
		}
		
	}
</script>
<style lang="scss" scoped>
.showModal{
	overflow: hidden;
}
.page {
	padding: 0 20rpx 20rpx 20rpx;
	.list{
		border-radius: 10rpx;
		box-shadow: 0px 4px 20px 0px rgba(0,0,0,0.1);
		padding: 12rpx 20rpx 20rpx 20rpx;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;
	}
}

// 按钮
.btn{
	position: fixed;
	bottom: 20rpx;
	width: 95%;
}
.slot-title{
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 30rpx 0;
}
.slot-content{
	padding: 0 30rpx 30rpx 30rpx;
	display: flex;
	justify-content: center;
	flex-direction: column;
	height: 50vh;
	
}
</style>
